{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 推荐方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "代码参考：https://blog.csdn.net/wsljqian/article/details/86602438 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "众数计算函数(定义域:实数)\n",
    "'''\n",
    "def descriptive_mode(list):\n",
    "    # [第1步] 获取 变量值列表 中 所有不重复的变量值\n",
    "    list_set=set(list);\n",
    "    # [第2步] 获取 所有不重复的变量值 在 变量值列表 中的 出现频数 \n",
    "    frequency_dict={};\n",
    "    for i in list_set:\n",
    "        frequency_dict[i]=list.count(i)\n",
    "    # [第3步] 获取 变量值列表 中 出现频数 最高的数值的 出现频数\n",
    "    max_frequency=max(frequency_dict.values())\n",
    "    mode_list=[]\n",
    "    if max_frequency==1: # 若最高的 出现频数 为1，则没有众数\n",
    "        return mode_list;\n",
    "    # [第4步] 找出 所有不重复的变量值 中 出现频数 最高的变量值\n",
    "    for key,value in frequency_dict.items():\n",
    "        if value==max_frequency:\n",
    "            mode_list.append(key)\n",
    "    return mode_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1.5]"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#调用 众数计算函数(descriptive_mode)\n",
    "mode=descriptive_mode([3,1,2,1.5,1.5,1.5]);\n",
    "mode"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 其他方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "代码参考：https://blog.csdn.net/sinat_28252525/article/details/80462437"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as numpy\n",
    "'''\n",
    "应用 numpy 现有方法的众数计算函数(定义域:整数)\n",
    "'''\n",
    "def descriptive_mode_numpy(list):\n",
    "    # [第1步] 获取 所有不重复的变量值 在 变量值列表 中的 出现频数 \n",
    "    frequency_dict=numpy.bincount(list)\n",
    "    # [第2步] 获取 出现频率 最高的变量值\n",
    "    return numpy.argmax(frequency_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1,1.5,1.5,1.5,2,3]的众数: 1\n",
      "[1,1,2,2,2,3]的众数: 2\n"
     ]
    }
   ],
   "source": [
    "#调用 应用 numpy 现有方法的众数计算函数(descriptive_mode_sumpy)\n",
    "mode=descriptive_mode_numpy([3,1,2,1.5,1.5,1.5]);\n",
    "print('[1,1.5,1.5,1.5,2,3]的众数:',mode)\n",
    "mode=descriptive_mode_numpy([3,2,2,2,1,1])\n",
    "print('[1,1,2,2,2,3]的众数:',mode)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
